<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="页面类型" prop="dictValue">
        <el-select v-model="queryParams.dictValue" placeholder="请选择" @change="handleQuery" clearable>
          <el-option
            v-for="dict in dict.type.page_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleGenHtml"
          v-hasPermi="['tool:gen:code']"
        >生成页面</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="htmlList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="页面类型"
        align="center"
        prop="dictValue"
        :show-overflow-tooltip="true"
        width="120"
      />
      <el-table-column
        label="页面类型名称"
        align="center"
        prop="dictLabel"
        :show-overflow-tooltip="true"
        width="120"
      />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            icon="el-icon-download"
            @click="handleGenHtml(scope.row)"
            v-hasPermi="['tool:gen:code']"
          >生成页面</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listHtml, genHtmlCode, batchGenHtmlCode } from "@/api/tool/gen";
import hljs from "highlight.js/lib/highlight";
import "highlight.js/styles/github-gist.css";

export default {
  name: "GenHtml",
  dicts: ['page_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 唯一标识符
      uniqueId: "",
      // 选中数组ID列表
      ids: [],
      // 选中数组列表
      rows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表数据
      htmlList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dictType: 'page_type',
        pageType: undefined,
      }
    };
  },
  created() {
    this.getList();
  },
  activated() {
    const time = this.$route.query.t;
    if (time != null && time != this.uniqueId) {
      this.uniqueId = time;
      this.queryParams.pageNum = Number(this.$route.query.pageNum);
      this.getList();
    }
  },
  methods: {
    /** 查询表集合 */
    getList() {
      this.loading = true;
      listHtml(this.queryParams).then(response => {
          this.htmlList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 生成HTML操作 */
    handleGenHtml(row) {
      if (row !== undefined && row.dictValue !== undefined) {
        genHtmlCode(row.dictValue).then(response => {
          this.$modal.msgSuccess("生成成功");
        });
      } else if (this.rows.length > 0) {
        batchGenHtmlCode(this.rows).then(response => {
          this.$modal.msgSuccess("生成成功");
        });
      } else {
        this.$modal.msgError("请选择要生成的数据");
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 高亮显示 */
    highlightedCode(code, key) {
      const vmName = key.substring(key.lastIndexOf("/") + 1, key.indexOf(".vm"));
      var language = vmName.substring(vmName.indexOf(".") + 1, vmName.length);
      const result = hljs.highlight(language, code || "", true);
      return result.value || '&nbsp;';
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.rows = selection.map(item => item.dictValue);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    }
  }
};
</script>